package com.open.party.cloud.view.home.jiCengDangJian.org.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.PartyOrgShowBean;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;
import com.sinothk.view.image.rounded.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

public class PartyOrgListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // 展示数据
    private ArrayList<PartyOrgShowBean> mData;
    private Context mContext;
    private OnSuperListener<PartyOrgShowBean> onSuperListener;

    public PartyOrgListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<PartyOrgShowBean> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<PartyOrgShowBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<PartyOrgShowBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        if (viewType == 1) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.party_org_list_item, viewGroup, false);
            return new ViewHolder(view);
        } else {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.party_org_list_item_user, viewGroup, false);
            return new ViewUserHolder(view);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // 绑定数据
        final PartyOrgShowBean itemData = mData.get(position);

        if (getItemViewType(position) == 1) {
            ViewHolder orgHolder = (ViewHolder) holder;
            orgHolder.userNameTv.setText(XUtils.string().getNotNullValue(itemData.getName()));

            if (onSuperListener != null) {
                orgHolder.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onSuperListener.onClick(position, itemData, "org");
                    }
                });
            }
        } else {
            ViewUserHolder userHolder = (ViewUserHolder) holder;
            userHolder.userNameTv.setText(XUtils.string().getNotNullValue(itemData.getName()));
            userHolder.phoneNumTv.setText(XUtils.string().getNotNullValue(itemData.getPhoneNum()));
            if (onSuperListener != null) {
                userHolder.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onSuperListener.onClick(position, itemData, "user");
                    }
                });
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView userNameTv, phoneNumTv, subTv;//, subTv, parStatusTv, durationTv, jianBaoTv, scoreTv;
        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            userNameTv = itemView.findViewById(R.id.userNameTv);
            phoneNumTv = itemView.findViewById(R.id.phoneNumTv);
            subTv = itemView.findViewById(R.id.subTv);

//            parStatusTv = itemView.findViewById(R.id.parStatusTv);
//            durationTv = itemView.findViewById(R.id.durationTv);
//            scoreTv = itemView.findViewById(R.id.scoreTv);
//
//            jianBaoTv = itemView.findViewById(R.id.jianBaoTv);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }

    static class ViewUserHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView userNameTv, phoneNumTv, subTv;//, subTv, parStatusTv, durationTv, jianBaoTv, scoreTv;
        RoundedImageView imageView;

        ViewUserHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            userNameTv = itemView.findViewById(R.id.userNameTv);
            phoneNumTv = itemView.findViewById(R.id.phoneNumTv);
            subTv = itemView.findViewById(R.id.subTv);

//            parStatusTv = itemView.findViewById(R.id.parStatusTv);
//            durationTv = itemView.findViewById(R.id.durationTv);
//            scoreTv = itemView.findViewById(R.id.scoreTv);
//
//            jianBaoTv = itemView.findViewById(R.id.jianBaoTv);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}